package com.zkt.auth.application.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.auth.application.convert.AuthPermissionDTOConverter;
import com.zkt.auth.application.dto.AuthPermissionDTO;
import com.zkt.auth.common.entity.Result;
import com.zkt.auth.domain.entity.AuthPermissionBO;
import com.zkt.auth.domain.service.AuthPermissionDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/22 15:52
 * @description 权限相关controller层
 **/

@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
	
	@Resource
	private AuthPermissionDomainService authPermissionDomainService;
	
	/**
	 * 新增权限
	 *
	 * @param authPermissionDTO 权限信息
	 * @return 是否成功
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
			}
			
			if (StrUtil.isBlank(authPermissionDTO.getName())) {
				throw new Exception("权限名称不能为空");
			}
			if (ObjUtil.isEmpty(authPermissionDTO.getParentId())) {
				throw new Exception("权限父id不能为空");
			}
			if (ObjUtil.isEmpty(authPermissionDTO.getType())) {
				throw new Exception("权限类型不能为空");
			}
			if (ObjUtil.isEmpty(authPermissionDTO.getPermissionKey())) {
				throw new Exception("权限标识不能为空");
			}
			
			AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
			
			return Result.ok(authPermissionDomainService.add(authPermissionBO));
		} catch (Exception e) {
			log.error("PermissionController.add.error:{}", e.getMessage(), e);
			return Result.fail("新增权限失败");
		}
	}
	
	/**
	 * 修改权限
	 *
	 * @param authPermissionDTO 权限信息
	 * @return 是否成功
	 */
	@PostMapping("update")
	public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
			}
			
			if (ObjUtil.isEmpty(authPermissionDTO.getId())) {
				throw new Exception("权限id不能为空");
			}
			if (StrUtil.isBlank(authPermissionDTO.getName())) {
				throw new Exception("权限名称不能为空");
			}
			
			AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
			
			return Result.ok(authPermissionDomainService.update(authPermissionBO));
		} catch (Exception e) {
			log.error("PermissionController.update.error:{}", e.getMessage(), e);
			return Result.fail("修改权限失败");
		}
	}
	
	/**
	 * 删除权限
	 *
	 * @param authPermissionDTO 权限信息
	 * @return 是否成功
	 */
	@PostMapping("delete")
	public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
			}
			
			if (ObjUtil.isEmpty(authPermissionDTO.getId())) {
				throw new Exception("权限id不能为空");
			}
			
			AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
			
			return Result.ok(authPermissionDomainService.delete(authPermissionBO));
		} catch (Exception e) {
			log.error("PermissionController.delete.error:{}", e.getMessage(), e);
			return Result.fail("删除权限失败");
		}
	}
	
	/**
	 * 权限启用/禁用
	 *
	 * @param authPermissionDTO 权限信息
	 * @return 是否成功
	 */
	@PostMapping("changeStatus")
	public Result<Boolean> changeStatus(@RequestBody AuthPermissionDTO authPermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.changeStatus.dto:{}", JSON.toJSONString(authPermissionDTO));
			}
			
			if (ObjUtil.isEmpty(authPermissionDTO.getId())) {
				throw new Exception("权限id不能为空");
			}
			if (ObjUtil.isEmpty(authPermissionDTO.getStatus())) {
				throw new Exception("权限状态不能为空");
			}
			
			AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
			
			return Result.ok(authPermissionDomainService.changeStatus(authPermissionBO));
		} catch (Exception e) {
			log.error("PermissionController.changeStatus.error:{}", e.getMessage(), e);
			return Result.fail("权限启用/禁用失败");
		}
	}
	
	/**
	 * 权限展示/隐藏
	 *
	 * @param authPermissionDTO 权限信息
	 * @return 是否成功
	 */
	@PostMapping("changeShowStatus")
	public Result<Boolean> changeShowStatus(@RequestBody AuthPermissionDTO authPermissionDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.changeShowStatus.dto:{}", JSON.toJSONString(authPermissionDTO));
			}
			
			if (ObjUtil.isEmpty(authPermissionDTO.getId())) {
				throw new Exception("权限id不能为空");
			}
			if (ObjUtil.isEmpty(authPermissionDTO.getShow())) {
				throw new Exception("权限展示状态不能为空");
			}
			
			AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
			
			return Result.ok(authPermissionDomainService.changeShowStatus(authPermissionBO));
		} catch (Exception e) {
			log.error("PermissionController.changeShowStatus.error:{}", e.getMessage(), e);
			return Result.fail("权限展示/隐藏失败");
		}
	}
	
	/**
	 * 查询用户权限
	 *
	 * @param userName 用户名称/账号
	 * @return 权限列表
	 */
	@GetMapping("getPermission")
	public Result<List<String>> getPermission(@RequestParam("userName") String userName) {
		try {
			if (log.isInfoEnabled()) {
				log.info("PermissionController.changeShowStatus.userName:{}", userName);
			}
			
			return Result.ok(authPermissionDomainService.getPermission(userName));
		} catch (Exception e) {
			log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
			return Result.fail("查询用户权限失败");
		}
	}
	
}
